package com.hoopchina.ui.home;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.util.ByteArrayBuffer;
import com.hoopchina.news.R;
import com.hoopchina.ui.main.BaseActivity;
import com.hoopchina.util.UrlUtils;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * DragListActivity
 * 
 * 新闻类别在水平条上的位置管理
 * @author Neusoft
 */

public class DragListActivity extends BaseActivity {
	
    public static final String            Id = "customItems";
    private static String[] newitems;
    
    private static List<String> list = null;
    private DragListAdapter adapter = null;
    
    public static List<String> groupKey= new ArrayList<String>();
    public static List<String> focusKey= new ArrayList<String>();
    private List<String> navList = new ArrayList<String>();
    private List<String> moreList = new ArrayList<String>();
   //确定要操作文件的文件名
    private final String FILE_NAME = "tempfile.txt";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drag_list_activity);
        
        try {
			initData();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        DragListView dragListView = (DragListView)findViewById(R.id.drag_list);
        dragListView.setDivider(null);
        adapter = new DragListAdapter(this, list);
        dragListView.setSelector(R.drawable.list_item__concern_selector);
        dragListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
        dragListView.setAdapter(adapter);
        mMyHandleBackButton = true;
    }
    


	@Override
    public void onResume() {
        super.onResume();
        mViewManager.hideLeftButton();
        mViewManager.setTitle(R.string.app_menu_custom_section);
        mViewManager.setStatus( "" );
//      mViewManager.setTabBarVisibility( false );
    }
    /*
     * 初始化新闻类别条目管理界面
     */
    public void initData() throws IOException{
        //数据结果
        list = new ArrayList<String>();
        
//        //groupKey存放的是分组标签
        groupKey.add(getResources().getString(R.string.app_home_draglistactivity_navigation));
        focusKey.add(getResources().getString(R.string.app_home_draglistactivity_focus));
        groupKey.add(getResources().getString(R.string.app_home_draglistactivity_more));

         File file=new File("/data/data/com.hoopchina/files/tempfile.txt");         
         if(file!=null && file.exists())
         {
        
        	Log.d("load", "load() in");
            //FileOutputStream fos = openFileOutput( FILE_NAME, Context.MODE_PRIVATE );
            InputStream is = null;
       		try {
       			is = openFileInput(FILE_NAME);
       		} catch (FileNotFoundException e) {
       			// TODO Auto-generated catch block
       			e.printStackTrace();
       		}//创建写入流
               BufferedInputStream bis = new BufferedInputStream(is);  
               ByteArrayBuffer baf = new ByteArrayBuffer(100);  
               int current = 0;  
               try {
       			while((current = bis.read()) != -1) {  
       			    baf.append((byte)current);  
       			}
       		} catch (IOException e) {
       			// TODO Auto-generated catch block
       			e.printStackTrace();
       		}  
              String myString = new String(baf.toByteArray());
              Log.d("--------filepath-----", myString );
              String substring = myString.substring(myString.lastIndexOf("[")+1, myString.lastIndexOf("]"));
              newitems = substring.split(", "); 
              Log.d("--------filepath-----", substring );
              int newItemsLen = newitems.length;
               for (int i = 0; i < newItemsLen; i++) {
            	   list.add( newitems[i] );
               }
         }
         else
         {
           for(int i=1; i<6; i++){
           navList.add(UrlUtils.HOME_ITEMS[i]);
           }
       
           list.add(getResources().getString(R.string.app_home_draglistactivity_navigation));
           list.add(getResources().getString(R.string.app_home_draglistactivity_focus));
           list.addAll(navList);
           int itemsLen=UrlUtils.HOME_ITEMS.length-1;
           for(int i=6; i<itemsLen; i++){
               moreList.add(UrlUtils.HOME_ITEMS[i]);
           }
           list.add(getResources().getString(R.string.app_home_draglistactivity_more));
           list.addAll(moreList);
         }       
    }
    /*
     * 把新闻类型管理的adapter写成一个内部类
     */
    public static class DragListAdapter extends ArrayAdapter<String>{

        public DragListAdapter(Context context, List<String> objects) {
            super(context, 0, objects);
        }
        
        public List<String> getList(){
            return list;
        }
        /*
         * 设定分组标签焦点标签不可选中操作
         * 
         */
        @Override
        public boolean isEnabled(int position) {
            if(groupKey.contains(getItem(position))){
                //如果是分组标签，返回false，不能选中，不能点击
                return false;
            }

            if(focusKey.contains(getItem(position))){
                //如果是分组标签，返回false，不能选中，不能点击
                return false;
            }
            
            return super.isEnabled(position);
        }
        /*
         * 设置列表相应的view
         * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            
            View view = convertView;
            if(groupKey.contains(getItem(position))){
                //如果是分组标签，就加载分组标签的布局文件，两个布局文件显示效果不同
                view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_tag, null);
            }else if (focusKey.contains(getItem(position)))
            {
            	view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_focus_item, null);
            }
            
            else{
                //如果是正常数据项标签，就加在正常数据项的布局文件
                view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item, null);
            }
            
            TextView textView = (TextView)view.findViewById(R.id.drag_list_item_text);
            textView.setText(getItem(position));
            
            return view;
        }
    }
    
    @Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		return false;
	}

	
}